package cn.johnyu.springboot;

import java.util.List;
import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import cn.johnyu.springboot.bean.User;
import cn.johnyu.springboot.dao.UserDao;


@SpringBootApplication
//引入其它配置类或配置文件的方式
//@ImportResource(locations="classpath:applicationContext.xml")
//@Import(value=MyConfig.class)
//@ComponentScan(basePackages= {"controller","dao","aspect"})
@EnableTransactionManagement // 启注解事务管理，等同于xml配置方式的 <tx:annotation-driven />
@EnableAspectJAutoProxy
public class MyConf {
	@Bean
	public DataSource createDataSource() {
		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
		//ds.setUrl("jdbc:mysql://60.205.223.43:3306/test");
		ds.setUrl("jdbc:mysql://localhost:3306/test");
		ds.setUsername("root");
		ds.setPassword("123");
		return ds;
	}
	@Bean
	public HibernateJpaVendorAdapter createVendorAdapter() {
		HibernateJpaVendorAdapter adapter=new HibernateJpaVendorAdapter();
		adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
		return adapter;
	}
	@Bean(name = "entityManagerFactory")
	public LocalContainerEntityManagerFactoryBean createEmfb() {
		Properties jpaProperties=new Properties();
		jpaProperties.setProperty("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
		jpaProperties.setProperty("hibernate.show_sql", "true");
		jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update");
		
		LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
		factoryBean.setDataSource(createDataSource());//即使多次调用，也只会返回由容器生成的DataSource(单例模式下)
		factoryBean.setPackagesToScan(new String[] { "cn.johnyu.springboot.bean"});
		factoryBean.setJpaVendorAdapter(createVendorAdapter());
		factoryBean.setJpaProperties(jpaProperties);
		return factoryBean;
	}
	@Bean
	public JpaTransactionManager createTxManager() {
		JpaTransactionManager txm=new JpaTransactionManager();
		txm.setEntityManagerFactory((EntityManagerFactory)createEmfb().getObject());
		return txm;
	}
	public static void main(String[] args) {
		SpringApplication.run(MyConf.class, args);
	}


}
